package com.fanbuchi.fukua.activity;


import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.fanbuchi.fukua.R;
import com.fanbuchi.fukua.activity.base.BaseLocActivity;
import com.fanbuchi.fukua.entity.UploadEntity;
import com.fanbuchi.fukua.log.AcsfLog;
import com.fanbuchi.fukua.manager.QiniuManager;
import com.fanbuchi.fukua.utils.QiniuUtils;

import java.io.File;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by rabbit on 2015/10/17.
 *
 * @title 拍照并且调整样式上传
 */
public class CameraActivity extends BaseLocActivity {

    private static final int requestCodeCamera=1122;
    private static final int requestCodePickPcicture=1123;

    @InjectView(R.id.picture)
    ImageView picture;
    @InjectView(R.id.camera)
    TextView camera;
    @InjectView(R.id.choice)
    TextView choice;
    @InjectView(R.id.upload)
    Button upload;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);
        ButterKnife.inject(this);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.reset(this);
    }

    @OnClick(R.id.camera)
    protected void onCamera() {
        Intent intent = new Intent(); //调用照相机
        intent.setAction(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(getPicturePath())));
        startActivityForResult(intent, requestCodeCamera);
    }

    @OnClick(R.id.choice)
    protected void onChoice() {
        Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,"image/*");
        startActivityForResult(intent, requestCodePickPcicture);
    }

    @OnClick(R.id.upload)
    protected void onUpload() {
        UploadEntity upload = new UploadEntity();
            upload.path=(String)picture.getTag();
       QiniuManager.getInstance().pciture(upload);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==RESULT_OK){
            if (requestCode==requestCodeCamera){
               String path= data.getExtras().getString(MediaStore.EXTRA_OUTPUT);
                AcsfLog.d("path="+path);
            }else if (requestCode==requestCodePickPcicture){
                Uri originalUri = data.getData();
                if (originalUri == null) {
                    return;
                }
                picture.setTag(getPath(originalUri));
                picture.setImageURI(originalUri);
            }
        }
    }
}
